/**
 * Created by Administrator on 2017-02-06.
 */
import jQuery from 'jquery'
let format = (value) => {
    return value >= 10 ? value : '0' + value
};

const today={

    //获取本地当天的
    init:function(type){
        let data = new Date(jQuery.ajax({async: false}).getResponseHeader("Date")),
            year= data.getFullYear(),
            month=data.getMonth() + 1,
            day=data.getDate(),
            hours=data.getHours(),
            minutes=data.getMinutes(),
            second=data.getSeconds(),
            result='';
        switch (type) {
            case "YY": // 2016
                result = `${year}`;
                break;
            case "MM": // 01
                result = `${format(month)}`;
                break;
            case "DD": // 05
                result = `${format(day)}`;
                break;
            case "YY-MM": // 2016-02
                result = `${year}-${format(month)}`;
                break;
            case "YY年MM月": // 2016-02
                result = `${year}年${format(month)}月`;
                break;
            case "MM-DD": // 01-05
                result = `${format(month)}-${format(day)}`;
                break;
            case "MM月DD日": // 01-05
                result = `${format(month)}月${format(day)}日`;
                break;
            case "hh:mm": // 11:12
                result = `${format(hours)}:${format(minutes)}`;
                break;
            case "YY-MM-DD": // 2015-01-05
                result = `${year}-${format(month)}-${format(day)}`;
                break;
            case "YY年MM月DD日": // 2015-01-05
                result = `${year}年${format(month)}月${format(day)}日`;
                break;
            case "YY-MM-DD hh:mm": // 2015-01-05 11:12
                result = `${year}-${format(month)}-${format(day)}  ${format(hours)}:${format(minutes)}`;
                break;
            case "YY年MM月DD日 hh:mm": // 2015-01-05 11:12
                result = `${year}年${format(month)}月${format(day)}日  ${format(hours)}:${format(minutes)}`;
                break;
            case "YY-MM-DD hh:mm:ss":// 2015-01-05 11:12:06
                result = `${year}-${format(month)}-${format(day)}  ${format(hours)}:${format(minutes)}:${format(second)}`;
                break;
            case "YY年MM月DD日 hh:mm:ss":// 2015-01-05 11:12:06
                result = `${year}年${format(month)}月${format(day)}日  ${format(hours)}:${format(minutes)}:${format(second)}`;
                break
        }
        return result;
    },
    //其他时间的格式
    otherTime:function(str,type="YY-MM-DD"){
        let year = str.getFullYear(),
            month = str.getMonth() + 1,
            day = str.getDate(),
            hours = str.getHours(),
            minutes = str.getMinutes(),
            second = str.getSeconds(),
            result = '';
        switch (type) {
            case "YY": // 2016
                result = `${year}`;
                break;
            case "MM": // 01
                result = `${format(month)}`;
                break;
            case "DD": // 05
                result = `${format(day)}`;
                break;
            case "YY-MM": // 2016-02
                result = `${year}-${format(month)}`;
                break;
            case "MM-DD": // 01-05
                result = `${format(month)}-${format(day)}`;
                break;
            case "hh:mm": // 11:12
                result = `${format(hours)}:${format(minutes)}`;
                break;
            case "hh:mm:ss": // 11:12
                result = `${format(hours)}:${format(minutes)}:${format(second)}`;
                break;
            case "YY-MM-DD": // 2015-01-05
                result = `${year}-${format(month)}-${format(day)}`;
                break;
            case "YY-MM-DD hh:mm": // 2015-01-05 11:12
                result = `${year}-${format(month)}-${format(day)}  ${format(hours)}:${format(minutes)}`;
                break;
            case "YY-MM-DD hh:mm:ss":// 2015-01-05 11:12:06
                result = `${year}-${format(month)}-${format(day)}  ${format(hours)}:${format(minutes)}:${format(second)}`;
                break
        }
        return result;
    },
    //获取年，月，日
    getYearDay:function(type,str){
        let result = '';
        switch (type) {
            case "YY": // 2016
                result = str.substring(0,4);
                break;
            case "MM": // 01
                result = str.substring(5,7);
                break;
            case "DD": // 05
                result = str.substring(8,10);
                break;
            case "YY-MM": // 2016-02
                result = str.substring(0,7);
                break;
            case "MM-DD": // 01-05
                result = str.substring(5,10);
                break;
        }
        return result;
    },
    //前天，明天，后天
    tmr:function(AddDayCount) {
        var dd = new Date();
        dd.setDate(dd.getDate()+AddDayCount);//获取AddDayCount天后的日期
        var y = dd.getFullYear();
        var m = format(dd.getMonth()+1);
        var d = format(dd.getDate());
        return y+"-"+m+"-"+d;
    },
    tmr2:function(AddDayCount,str) {
        var dd = new Date(str.replace(/-/g,'/'));
        dd.setDate(dd.getDate()+AddDayCount);//获取AddDayCount天后的日期
        var y = dd.getFullYear();
        var m = format(dd.getMonth()+1);
        var d = format(dd.getDate());
        return y+"-"+m+"-"+d;
    },
    //获取上一个月
    getPreMonth:function (date) {
        let arr = date.split('-');
        let year = arr[0]; //获取当前日期的年份
        let month = arr[1]; //获取当前日期的月份
        let year2 = year;
        let month2 = parseInt(month) - 1;
        if (month2 == 0) {
            year2 = parseInt(year2) - 1;
            month2 = 12;
        }
        month2 = format(month2);
        var t2 = year2 + '-' + month2 ;
        return t2;
    },
    //获取下一个月
    getNextMonth:function (date) {
        var arr = date.split('-');
        var year = arr[0]; //获取当前日期的年份
        var month = arr[1]; //获取当前日期的月份
        var year2 = year;
        var month2 = parseInt(month) + 1;
        if (month2 == 13) {
            year2 = parseInt(year2) + 1;
            month2 = 1;
        }
        month2 = format(month2);
        var t2 = year2 + '-' + month2;
        return t2;
    },
    //某月最后一天
    getLastDay:function (y,m) {
        let d = 30;
        m = Number(m);
        switch (m) {
            case 1:
            case 3:
            case 5:
            case 7:
            case 8:
            case 10:
            case 12:
                d = 31;
                break;
            case 4:
            case 6:
            case 9:
            case 11:
                d = 30;
                break;
            case 2:
                d = 28;
                if ((y % 4 == 0) && (y % 100 != 0) || (y % 400 == 0)) {
                    d = 29;
                }
                break;
        }
        return d;//获取当月最后一天日期
    },
    //本月前一个月的最后一天
    getPreLastDay:function(){
        let y = this.init('YY');
        let m = new Date().getMonth();
        let d = new Date(y,m,0);
        if(m >= 10){
            m = m;
        }else {
            m = '0' + m;
        }
        return y + m + d.getDate();
    },
    //根据出生年月日算年龄
    ages:function (str) {
        var   r   =   str.match(/^(\d{1,4})(-|\/)(\d{1,2})\2(\d{1,2})$/);
        if(r==null)return   false;
        var   d=   new   Date(r[1],   r[3]-1,   r[4]);
        if   (d.getFullYear()==r[1]&&(d.getMonth()+1)==r[3]&&d.getDate()==r[4])
        {
            var   Y   =   new   Date().getFullYear();
            return Y-r[1];
        }
        return("输入的日期格式错误！");
    },
    //转时间戳
    getTime:function(day){
        let re = /(\d{4})(?:-(\d{1,2})(?:-(\d{1,2}))?)?(?:\s+(\d{1,2}):(\d{1,2}):(\d{1,2}))?/.exec(day);
        return new Date(re[1],(re[2]||1)-1,re[3]||1,re[4]||0,re[5]||0,re[6]||0).getTime()/1000;
    },
    getTime2:function(string){
        if(!string){
            return ''
        }
        let t = new Date(string.replace(/-/g,'/'));
        let ux = t.getTime()/1000;
        return ux
    }
};
export default today

//2021.5.28 新写时间的一些方法
//时间格式,默认今天
export const timeFormat=(time,type='ymd',space='/')=>{
    time = time? new Date(time.replace(/-/g,'/')) : new Date()
    let year = time.getFullYear(),
        month = time.getMonth() + 1,
        day = time.getDate(),
        hours = time.getHours(),
        minutes = time.getMinutes(),
        second = time.getSeconds(),
        result = '';
    switch (type) {
        case "y": // 2016
            result = `${year}`;
            break;
        case "m": // 01
            result = `${format(month)}`;
            break;
        case "d": // 05
            result = `${format(day)}`;
            break;
        case "ym": // 2016-02
            result = `${year}${space}${format(month)}`;
            break;
        case "md": // 01-05
            result = `${format(month)}${str}${format(day)}`;
            break;
        case "hm": // 11:12
            result = `${format(hours)}:${format(minutes)}`;
            break;
        case "hms": // 11:12
            result = `${format(hours)}:${format(minutes)}:${format(second)}`;
            break;
        case "ymd": // 2015-01-05
            result = `${year}${space}${format(month)}${space}${format(day)}`;
            break;
        case "ymd hm": // 2015-01-05 11:12
            result = `${year}${space}${format(month)}${space}${format(day)}  ${format(hours)}:${format(minutes)}`;
            break;
        case "ymd hms":// 2015-01-05 11:12:06
            result = `${year}${space}${format(month)}${space}${format(day)}  ${format(hours)}:${format(minutes)}:${format(second)}`;
            break
    }
    return result;
}
//某一天的前后日期
export const getCountDay=(AddDayCount,dd=new Date(),space='/')=>{
    dd.setDate(dd.getDate()+AddDayCount);
    var y = dd.getFullYear();
    var m = format(dd.getMonth()+1);
    var d = format(dd.getDate());
    return y+space+m+space+d;
}
